package com.hqyj.controller;


import com.hqyj.entity.User;
import com.hqyj.vo.ResultInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LoginController {

    @GetMapping("/index")
    String login(){
        return "chjQianDuan/index";
    }



    @PostMapping("/login")
    @ResponseBody
    ResultInfo doLogin(String userName, String password){

        System.out.println("进入登录验证");
        Subject subject = SecurityUtils.getSubject();

        System.out.println("前端传过来的值:name:"+userName+",password:"+password);
        UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
        try{
            subject.login(token);
        }catch(Exception e){
            System.out.println("");
            System.out.println(e.getMessage()+"%%%%%%%%%%%%%");
            User user = (User) subject.getSession().getAttribute("u");

            if(user == null){
                return new ResultInfo("500","账号不存在！");
            }else{
                return new ResultInfo("500","密码输入错误！");
            }

        }

        return new ResultInfo("200","success！");  //这里应当重定向到后台路径去
    }

    @GetMapping("/goLogin")
    String goLogon(){
        return "loginRelate/login";
    }


    @RequestMapping("/mianban")
    @RequiresRoles("经理")
    String goMianBan(){
        return"loginRelate/mianban";
    }
    @RequestMapping("/a")
    String goabout(){
        return"chjQianDuan/about";
    }
}
